package com.example.function.sys.model.pojo;

import com.example.common.support.ApplicationContextProvider;
import com.example.function.sys.model.one.*;
import com.example.function.sys.service.ISysDepartmentService;
import com.example.function.sys.service.ISysPostService;
import com.example.function.sys.service.ISysSupplierService;
import com.example.function.sys.service.ISysUserService;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <h1>系统用户数据对象</h1>
 * Created by hanqf on 2021/7/21 17:55.
 */

@Data
public class SysUserPojo implements Serializable {
    private static final long serialVersionUID = -6402882005939821025L;
    /*
     * 主键id
     */
    private Long id;


    /*
     * 登录帐号，供应商邮箱
     */
    private String userLoginName;


    /*
     * 密码
     */
    @JsonIgnore
    private String userLoginPassword;


    /*
     * 姓名，供应商全称
     */
    private String userName;


    /*
     * 联系方式，供应商手机号
     */
    private String userPhone;


    /*
     * 部门
     */
    private SysDepartmentPojo department;


    /*
     * 岗位
     */
    private SysPostPojo post;


    /*
     * 用户类型：0-后台用户，1-供应商用户
     */
    private Integer userType;


    /*
     * 状态：0-禁用，1-启用
     */
    private Integer userStatus;


    /*
     * 创建时间
     */
    private java.util.Date userCreatetime;


    /*
     * 修改时间
     */
    private java.util.Date userModifytime;


    /*
     * 用户角色列表
     */
    private List<SysRole> roles;

    /*
     * 供应商
     */
    private SysSupplierPojo supplier;


    public static SysUserPojo wrapper(SysUser sysUser) {
        SysUserPojo sysUserPojo = new SysUserPojo();
        BeanUtils.copyProperties(sysUser, sysUserPojo);

        //封装角色
        final ISysUserService sysUserService = (ISysUserService) ApplicationContextProvider.getBean("sysUserService");
        final List<SysRole> roles = sysUserService.findRolesByUserId(sysUser.getId());
        sysUserPojo.setRoles(roles);

        //系统用户封装部门和岗位
        if (sysUser.getUserType() == 0) {
            //封装部门
            if (sysUser.getUserDepartId() != null) {
                final ISysDepartmentService sysDepartmentService = (ISysDepartmentService) ApplicationContextProvider.getBean("sysDepartmentService");
                final SysDepartment department = sysDepartmentService.findById(sysUser.getUserDepartId());
                sysUserPojo.setDepartment(SysDepartmentPojo.wrapper(department));
            }

            //封装岗位
            if (sysUser.getUserPostId() != null) {
                final ISysPostService sysPostService = (ISysPostService) ApplicationContextProvider.getBean("sysPostService");
                final SysPost post = sysPostService.findById(sysUser.getUserPostId());
                sysUserPojo.setPost(SysPostPojo.wrapper(post));
            }

        } else { //封装供应商数据
            final ISysSupplierService sysSupplierService = (ISysSupplierService) ApplicationContextProvider.getBean("sysSupplierService");
            final SysSupplier supplier = sysSupplierService.findByUserId(sysUser.getId());
            sysUserPojo.setSupplier(SysSupplierPojo.wrapper(supplier));
        }
        return sysUserPojo;
    }
}
